<?php defined('BASEPATH') OR exit('No direct script access allowed');

	class promo_class extends CI_Model {
	
		public function promo_list(){
			if(isset($post['intID'])){
				$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
				$this->db->order_by("intID", "asc");
				$query = $this->db->get_where('jtn_promos',array('intDisabled'=>0,'intID'=>$intID));
			} else {
				$this->db->order_by("intID", "asc");
				$query = $this->db->get_where('jtn_promos',array('intDisabled'=>0));
			}
			if ($query->num_rows() > 0){
				$result = $query->result_array();
				return $result;
			}
			else{
				return 0;
			}
		}

		public function update($post){
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			$strTitle= stripslashes(filter_var($post['strTitle'], FILTER_SANITIZE_STRING));
			$strDescription= stripslashes(filter_var($post['strDescription'], FILTER_SANITIZE_STRING));
			$dateStart= stripslashes(filter_var($post['dateStart'], FILTER_SANITIZE_STRING));
			$dateEnd= stripslashes(filter_var($post['dateEnd'], FILTER_SANITIZE_STRING));
			$strPromoCode = stripslashes(filter_var($post['strPromoCode'], FILTER_SANITIZE_STRING));
			$intPoints = stripslashes(filter_var($post['intPoints'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'strTitle' => $strTitle,
					'strDescription' => $strDescription,
					'dateStart' => $dateStart,
					'dateEnd' => $dateEnd,
					'strPromoCode' => $strPromoCode,
					'intPoints' => $intPoints,
					'strModifiedBy'=> $this->session->userdata('intID'),
					'dateModifiedDate'=> date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_promos', $data);
			
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
		public function get_promo($post){
			if(isset($post['intID'])){
				$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
				$this->db->where(array('intID'=>$intID));
			} 
			$query = $this->db->get('jtn_promos')->result_array();
			$query = current($query);
		
			if(trim($query['intID'])=="") {
				return 0;
			} else {
				return $query;
			}
		}
		public function delete($post){
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));	
		
			$data = array(
					'intDisabled' => 1,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
				
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_promos', $data);
				
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
		public function insert($post){
			$strTitle= stripslashes(filter_var($post['strTitle'], FILTER_SANITIZE_STRING));
			$strDescription= stripslashes(filter_var($post['strDescription'], FILTER_SANITIZE_STRING));
			$dateStart= stripslashes(filter_var($post['dateStart'], FILTER_SANITIZE_STRING));
			$dateEnd= stripslashes(filter_var($post['dateEnd'], FILTER_SANITIZE_STRING));
			$strPromoCode = stripslashes(filter_var($post['strPromoCode'], FILTER_SANITIZE_STRING));
			$intPoints = stripslashes(filter_var($post['intPoints'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'strTitle' => $strTitle,
					'strDescription' => $strDescription,
					'dateStart' => $dateStart,
					'dateEnd' => $dateEnd,
					'strPromoCode' => $strPromoCode,
					'intPoints' => $intPoints,
					'dateCreatedDate'=> date('Y-m-d H:i:s'),
					'strCreatedBy'=> $this->session->userdata('intID'),
					'strModifiedBy'=> $this->session->userdata('intID'),
					'dateModifiedDate'=> date('Y-m-d H:i:s')
			);

			$this->db->insert('jtn_promos', $data);

			//$message['qry']=$this->db->last_query();
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
				generated_qrcode($strPromoCode, $this->db->insert_id());
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
		
		//Promo Mobile Services
		public function get_promo_mobile_class($post){
			if(isset($post['intID'])){
				$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
				$this->db->where(array('intID'=>$intID));
			} 
			$query = $this->db->get('jtn_promos')->result_array();
			$query = current($query);
		
			if(trim($query['intID'])=="") {
				return 0;
			} else {
				return $query;
			}
		}
		public function update_promo_mobile_class($post){
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			$intUserID = stripslashes(filter_var($post['intUserID'], FILTER_SANITIZE_STRING));
			$intPoints = stripslashes(filter_var($post['intPoints'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'intStatus' => $intStatus,
					'strModifiedBy'=> $this->session->userdata('intID'),
					'dateModifiedDate'=> date('Y-m-d H:i:s')
			);
			
/* 			$data2 = array(
					'intPoints' => $intPoints, 
					'strModifiedBy'=> $this->session->userdata('intID'),
					'dateModifiedDate'=> date('Y-m-d H:i:s')
			); */
			
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_promos', $data);
			
			
			
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
	} // end class
?>